package fragment;


import android.content.Context;
import android.graphics.Color;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.view.ViewPager;
import android.util.TypedValue;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.HorizontalScrollView;
import android.widget.LinearLayout;
import android.widget.TextView;

import java.util.ArrayList;
import java.util.List;

import bean.GridViewSpeakerBean;
import bean.SpeakerBean;
import qf.wlh.com.oneprisdent.R;
import qf.wlh.com.oneprisdent.adapter.SpeakerAdapter;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;
import utils.RetrofitSpeakerDetailFragmentInterface;
import utils.RetrofitSpeakerFragmentInterface;

import static qf.wlh.com.oneprisdent.R.id.speaker_linearlayout;

/**
 * A simple {@link Fragment} subclass.
 */
public class SpeakerFragment extends Fragment {
    private Retrofit retrofit;
    private HorizontalScrollView horizontal;
    private LinearLayout linearLayout;
    private ViewPager viewPager;
    private FragmentManager fragmentManager;
    private List<SpeakerBean.DataBean> dataBean=new ArrayList<>();
    private List<String> list=new ArrayList<>();

    private List<TextView> textViews =new ArrayList<>();
    private List<String>idList=new ArrayList<>();
    private List<GridViewSpeakerBean.DataBean> data=new ArrayList<>();
    private SpeakerAdapter adapter;

    private TextView textView;
    public SpeakerFragment() {
        // Required empty public constructor

    }

    @Override
    public void onAttach(Context context) {
        super.onAttach(context);
        fragmentManager=getActivity().getSupportFragmentManager();
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View view=inflater.inflate(R.layout.fragment_speaker, container, false);

        initView(view);
        initData();

        return view;
    }
    private void initView(View view) {
        horizontal = ((HorizontalScrollView) view.findViewById(R.id.scrollview));
        linearLayout = ((LinearLayout) view.findViewById(speaker_linearlayout));
        viewPager = ((ViewPager) view.findViewById(R.id.speakerviewpager));
    }

    private void initData() {
        retrofit=new Retrofit.Builder()
                .baseUrl("http://api.yixi.tv/api/")
                .addConverterFactory(GsonConverterFactory.create())
                .build();
        RetrofitSpeakerFragmentInterface retrofitSpeakerFragmentInterface=retrofit.create(RetrofitSpeakerFragmentInterface.class);
        retrofitSpeakerFragmentInterface.getData().enqueue(new Callback<SpeakerBean>(){

            @Override
            public void onResponse(retrofit2.Call<SpeakerBean> call, Response<SpeakerBean> response) {
                 dataBean =  response.body().getData();

                if (dataBean!=null){
                    for (int i=0;i<dataBean.size();i++){
                        textView=new TextView(getContext());
                        textView.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT,
                                ViewGroup.LayoutParams.WRAP_CONTENT));

                        textView.setText(dataBean.get(i).getName());
                        textView.setTextColor(Color.WHITE);
                        textView.setTextSize(TypedValue.COMPLEX_UNIT_SP, 22);
                        textView.setPadding(30, 7, 30, 7);
                        textView.setTag(i);
                        idList.add(dataBean.get(i).getId()+"");
                        textView.setOnClickListener(new View.OnClickListener() {

                            @Override
                            public void onClick(View view) {
                                int position= (int) view.getTag();

                                String ss=dataBean.get(position).getId()+"";
                                initViewPagerData(ss);
                                viewPager.setCurrentItem(position);
                            }


                        });
                        //默认第一个选中
                        initViewPagerData(dataBean.get(0).getId()+"");
                        textViews.add(textView);
                        linearLayout.addView(textView);
                        //获取相应的id

                    }

                }
            }

            @Override
            public void onFailure(retrofit2.Call<SpeakerBean> call, Throwable t) {

            }
        });


    }
    private void initViewPagerData(String id) {

        retrofit=new Retrofit.Builder()
                .baseUrl("http://api.yixi.tv/api/v1/category/"+id+"/")
                .addConverterFactory(GsonConverterFactory.create())
                .build();
        RetrofitSpeakerDetailFragmentInterface retrofitSpeakerDetailFragmentInterface
                =retrofit.create(RetrofitSpeakerDetailFragmentInterface.class);
        retrofitSpeakerDetailFragmentInterface.getData().enqueue(new Callback< GridViewSpeakerBean>(){
            @Override
            public void onResponse(Call<GridViewSpeakerBean> call, Response<GridViewSpeakerBean> response) {
                data =  response.body().getData();

                if (data!=null){

                    adapter=new SpeakerAdapter( data,getActivity(),fragmentManager);
                    viewPager.setAdapter(adapter);
                    adapter.notifyDataSetChanged();

                }
            }

            @Override
            public void onFailure(Call<GridViewSpeakerBean> call, Throwable t) {

            }
        });
        viewPager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {

                //先全部设置为可以点击
                for (TextView textView :textViews) {
                    textView.setEnabled(true);
                }
                //把最后选中的设置为不可点击
                TextView textView  = textViews.get(position);

                textView.setEnabled(false);


                int left=textView.getLeft();    //控件左侧的位置
                int width=textView.getWidth();   //控件宽度
                int widthPixels  = getResources().getDisplayMetrics().widthPixels; //屏幕的宽度

                int dx=(widthPixels/2-left-width/2);
                //水平的滚动视图要挪动
                //负 往右
                //正 往左

                horizontal.smoothScrollTo(dx*-1,0);

                String ss=dataBean.get(position).getId()+"";
                initViewPagerData(ss);

            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });
    }

}
